package mcjunkin;
import mcjunkin.gui.*;
import javax.swing.*;
import java.io.*;

public class Start{


	public static void main(String[] args) throws Exception{
	
		//use the system look and feel if possible
		//try{
			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			//UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
			//UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
		//}catch(Exception ex){ex.printStackTrace();}
		
		//create and show the GUI
		final MainFrame jmgui = new MainFrame();
		jmgui.setTitle("McJunkin 1.0");
		jmgui.setSize(500,500);
		jmgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jmgui.setVisible(true);
		
		//create a MatchFinder object for actually running the match algorithm
		final MatchListener ml = jmgui.getOutputWidget();
		final ProgressListener pl = jmgui.getControlWidget();
		final StatusListener sl = jmgui.getStatusWidget();
		final MatchFinder mf = new MatchFinder(ml,pl,sl);

		//set start action
		jmgui.getControlWidget().setStartAction(new Runnable(){
			public void run(){
				try{
					jmgui.getOutputWidget().removeAll();
					mf.start( jmgui.getControlWidget().getFolder(),
						new IgnoreFilter(jmgui.getControlWidget().getIgnore()));
				}
				//folder entered is not a folder (or does not exist)
				catch(FileNotFoundException fnfe){
					String msg = jmgui.getControlWidget().getFolder()+
						" is not a directory.";
					JOptionPane.showMessageDialog(jmgui, msg);
				}
				catch(Exception ex){
					//sl.message(ex.toString());
					JOptionPane.showMessageDialog(jmgui, ex.toString());
					ex.printStackTrace();
				}
			}
		});

		//set stop action
		jmgui.getControlWidget().setStopAction(new Runnable(){
			public void run(){
				mf.stop();
			}
		});


		//set save action
		jmgui.getControlWidget().setSaveAction(new Runnable(){
			public void run(){
				try{
					//get a file
					JFileChooser chooser = new JFileChooser(
						System.getProperty("user.dir"));  
					int returnVal = chooser.showSaveDialog(null);  
					if(returnVal != JFileChooser.APPROVE_OPTION) return ;
					if(chooser.getSelectedFile()==null) return ;  
					File file = chooser.getSelectedFile(); 
					//open the file
					FileOutputStream fos = new FileOutputStream(file);
					//get the string representation of the output widget contents
					String rep = jmgui.getOutputWidget().getStringRepresentation();
					byte[] bytes = rep.getBytes();
					int pos = 0;
					//write bytes to file
					int WRITE_BLOCK_SIZE=8192;
					while(pos < bytes.length){
						int block = Math.min(bytes.length-pos,WRITE_BLOCK_SIZE);
						fos.write(bytes,pos,block);
						pos += block;
						sl.message("Writing ("+Lib.readableFileSize(pos)+"/"+
							Lib.readableFileSize(bytes.length)+")");
						//Thread.currentThread().sleep(1000);
					}
					sl.message("Wrote to "+file.getName());
					//close the file
					fos.close();
				}
				catch(Exception ex){
					//sl.message(ex.toString());
					JOptionPane.showMessageDialog(jmgui, ex.toString());
					ex.printStackTrace();
				}
			}
		});

		/* Temporary Code to test add/remove methods of OutputWidget */
		/*
		int delay=4000;
		System.out.println("Adding stuff");
		ml.addMatch("one", "mone");
		Thread.currentThread().sleep(delay);
		ml.addMatch("two", "mtwo");
		ml.addMatch("two", "mtwo2");
		Thread.currentThread().sleep(delay);
		ml.addMatch("three", "mthree");
		Thread.currentThread().sleep(delay);
		ml.addMatch("mfour", "mfour");
		Thread.currentThread().sleep(delay);
		ml.removeMatch("one", "mone");
		Thread.currentThread().sleep(delay);
		ml.removeMatch("two", "mtwo");
		Thread.currentThread().sleep(delay);
		ml.removeMatch("three", "mthree");
		Thread.currentThread().sleep(delay);
		ml.removeMatch("mfour", "mfour");
		Thread.currentThread().sleep(delay);
		ml.removeMatch("two", "mtwo");
		*/
		/* END Temporary Code to test add/remove methods of OutputWidget */
	
	}

}
